﻿using DeMeng.Models.Base;
using DeMeng.Models.PageModel;
using DeMeng.Repository.BaseRepository;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DeMeng.Services.BaseServices
{
    public class BaseServices<TEntity> : IBaseServices<TEntity>
            where TEntity : BaseEntity, new()
    {

        public IBaseRepository<TEntity> BaseDal;//通过在子类的构造函数中注入，这里是基类，不用构造函数


        public async Task<int> Add(TEntity model)
        {
            return await BaseDal.Add(model);
        }

        public async Task<int> Add(List<TEntity> models)
        {
            return await BaseDal.Add(models);
        }

        public async Task<bool> Delete(TEntity model)
        {
            return await BaseDal.Delete(model);
        }

        public async Task<bool> DeleteById(object id)
        {
            return await BaseDal.DeleteById(id);
        }

        public async Task<bool> DeleteByIds(object[] ids)
        {
            return await BaseDal.DeleteByIds(ids);
        }

        public async Task<TEntity> GetEntity(Expression<Func<TEntity, bool>> whereExpression, string orderString = null)
        {
            return await BaseDal.GetEntity(whereExpression,   orderString = null);
        }

        public async Task<PageResultModel<TModel>> Pages<TModel>(BasePageQuery<TEntity> query)
        {
            return await BaseDal.Pages<TModel>(query);
        }

        public async Task<PageResultModel<TResult>> PagesMuch<T, T2, T3, TResult>(BasePageQuery<TEntity> query, Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
        {
            return await BaseDal.PagesMuch<T, T2, T3, TResult>(query, joinExpression, selectExpression, whereLambda = null);
        }

        public async Task<PageResultModel<TResult>> PagesMuch<T, T2, TResult>(BasePageQuery<TEntity> query, Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            return await BaseDal.PagesMuch<T, T2, TResult>(query,  joinExpression,selectExpression, whereLambda = null);
        }

        public async Task<List<TEntity>> Query()
        {
            return await BaseDal.Query();
        }

        public async Task<List<TEntity>> Query(string strWhere)
        {
            return await BaseDal.Query(strWhere);
        }

        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, bool IsDelete = false)
        {
            return await BaseDal.Query(whereExpression, IsDelete = false);
        }

        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await BaseDal.Query(whereExpression, strOrderByFileds);
        }

        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await BaseDal.Query(whereExpression, orderByExpression, isAsc = true);
        }

        public async Task<List<TEntity>> Query(string strWhere, string strOrderByFileds)
        {
            return await BaseDal.Query(strWhere,  strOrderByFileds);
        }

        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return await BaseDal.Query(whereExpression, intTop, strOrderByFileds);
        }

        public async Task<List<TEntity>> Query(string strWhere, int intTop, string strOrderByFileds)
        {
            return await BaseDal.Query(strWhere, intTop,   strOrderByFileds);
        }

        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await BaseDal.Query(whereExpression, intPageIndex, intPageSize, strOrderByFileds);
        }

        public async Task<List<TEntity>> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await BaseDal.Query(  strWhere,   intPageIndex,   intPageSize,   strOrderByFileds);
        }


        public async Task<TEntity> QueryById(object objId)
        {
            return await BaseDal.QueryById(objId);
        }

        public async Task<TEntity> QueryById(object objId, bool blnUseCache = false)
        {
            return await BaseDal.QueryById(objId,blnUseCache=false);
        }

        public async Task<List<TEntity>> QueryByIds(object[] lstIds)
        {
            return await BaseDal.QueryByIds(lstIds);
        }

        public async Task<int> QueryCount(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await BaseDal.QueryCount(whereExpression);
        }

        public async Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null, string orderString = null) where T : class, new()
        {
            return await BaseDal.QueryMuch<T, T2, T3, TResult>(joinExpression, selectExpression, whereLambda = null, orderString = null);
        }

        public async Task<List<TResult>> QueryMuch<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null, string orderString = null) where T : class, new()
        {
            return await BaseDal.QueryMuch<T, T2, TResult>(joinExpression, selectExpression, whereLambda = null, orderString = null);
        }

        public async Task<PageResultModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, string strOrderByFileds = null)
        {
            return await BaseDal.QueryPage(whereExpression, pageIndex, pageSize, strOrderByFileds);
        }

        public async Task<bool> Update(TEntity model)
        {
            return await BaseDal.Update(model);
        }

        public async Task<bool> Update(TEntity entity, string strWhere)
        {
            return await BaseDal.Update(entity, strWhere);
        }

        public async Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string strWhere = "")
        {
            return await BaseDal.Update(entity, lstColumns, lstColumns, strWhere);
        }

        public async Task<bool> Update( Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> expression)
        {
            return await BaseDal.Update(columns, expression);
        } 
    }
}
